package yawning.mew.cat;

import yawning.mew.cat.CatModelSimulation.SimulatorProperty;
import yawning.mew.cat.CatModelSimState.CatTargetData;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationEventAction;

public class EventFeralFF extends SimulationEventAction {
	CatModelSimState mState;
	private CatTargetData mTargetData;
	private String mTargetName;
	private boolean mExternal;
	int mStacks;

	EventFeralFF(CatModelSimState state, CatTargetData targetData) {
		super(state.mSimEngine);
		mState = state;
		mTargetData = targetData;
		mTargetName = targetData.getSimulationTarget().getTarget().getName();
		mExternal = mState.mSimEngine.getConfig().getBool(SimulatorProperty.ASSUMEEXTERNALFF);
		if (mExternal) {
			if (mState.mIsLogging) mState.mLog.log("Feral FF: %s External\n", mTargetName);

			mStacks = 3;
			schedule(Double.MAX_VALUE);
		}

		mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
	}

	@Override
	public double perform() {
		if (!mExternal) {
			AttackResult result = mState.mModel.getSpellResult();

			switch (result) {
			case MISS:
				mNrFFMiss++;
				break;
			case HIT:
			case CRIT:
				mNrFFHit++;

				// Recalculate armor here.
				mStacks = Math.min(3, mStacks + mState.mModel.nrFeralFFStacksPerCast);
				mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
				schedule(mState.mModel.feralFFDebuffDuration);

				break;
			}

			if (mState.mIsLogging) {
				if (result == AttackResult.MISS) {
					mState.mLog.log("Feral FF: %s MISS Stacks: %d\n", mTargetName, mStacks);
				} else {
					mState.mLog.log("Feral FF: %s HIT Stacks: %d\n", mTargetName, mStacks);
				}
			}
		}

		return 1.0;
	}

	@Override
	public void onExpire() {
		if (mState.mIsLogging) mState.mLog.log("Feral FF: %s Expiration\n", mTargetName);

		mStacks = 0;
		mTargetData.setArmorMajorMult(1 - 0.04 * mStacks);
	}

	int mNrFFMiss = 0;
	int mNrFFHit = 0;
}